home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / sql-bench / test-transactions < prev    next >
Encoding:
Text File  |  2004-10-28  |  7.0 KB  |  299 lines

  1. #!/usr/bin/perl
  2. # Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Library General Public
  15. # License along with this library; if not, write to the Free
  16. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. # MA 02111-1307, USA
  18. #
  19. # Test of transactions performance.
  20. #
  21.  
  22. ##################### Standard benchmark inits ##############################
  23.  
  24. use Cwd;
  25. use DBI;
  26. use Benchmark;
  27. #use warnings;
  28.  
  29. $opt_groups=27;            # Characters are 'A' -> Z
  30.  
  31. $opt_loop_count=10000;        # Change this to make test harder/easier
  32. $opt_medium_loop_count=100; # Change this to make test harder/easier
  33.  
  34. $pwd = cwd(); $pwd = "." if ($pwd eq '');
  35. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  36.  
  37. # Avoid warnings for variables in bench-init.pl
  38. # (Only works with perl 5.6)
  39. #our ($opt_small_test, $opt_small_tables, $opt_debug, $opt_force);
  40.  
  41. if ($opt_small_test || $opt_small_tables)
  42. {
  43.   $opt_loop_count/=100;
  44.   $opt_medium_loop_count/=10;
  45. }
  46.  
  47.  
  48. if (!$server->{transactions} && !$opt_force)
  49. {
  50.   print "Test skipped because the database doesn't support transactions\n";
  51.   exit(0);
  52. }
  53.  
  54. ####
  55. ####  Connect and start timeing
  56. ####
  57.  
  58. $start_time=new Benchmark;
  59. $dbh = $server->connect();
  60.  
  61. ###
  62. ### Create Table
  63. ###
  64.  
  65. print "Creating tables\n";
  66. $dbh->do("drop table bench1");
  67. $dbh->do("drop table bench2");
  68.  
  69. do_many($dbh,$server->create("bench1",
  70.                  ["idn int NOT NULL",
  71.                   "rev_idn int NOT NULL",
  72.                   "region char(1) NOT NULL",
  73.                   "grp int NOT NULL",
  74.                   "updated tinyint NOT NULL"],
  75.                  ["primary key (idn)",
  76.                   "unique (region,grp)"]));
  77. do_many($dbh,$server->create("bench2",
  78.                  ["idn int NOT NULL",
  79.                   "rev_idn int NOT NULL",
  80.                   "region char(1) NOT NULL",
  81.                   "grp int NOT NULL",
  82.                   "updated tinyint NOT NULL"],
  83.                  ["primary key (idn)",
  84.                   "unique (region,grp)"]));
  85.  
  86. $dbh->{AutoCommit} = 0;
  87.  
  88. ###
  89. ### Test insert perfomance
  90. ###
  91.  
  92. test_insert("bench1","insert_commit",0);
  93. test_insert("bench2","insert_autocommit",1);
  94.  
  95. sub test_insert
  96. {
  97.   my ($table, $test_name, $auto_commit)= @_;
  98.   my ($loop_time,$end_time,$id,$rev_id,$grp,$region);
  99.  
  100.   $dbh->{AutoCommit}= $auto_commit;
  101.   $loop_time=new Benchmark;
  102.  
  103.   for ($id=0,$rev_id=$opt_loop_count-1 ; $id < $opt_loop_count ;
  104.        $id++,$rev_id--)
  105.   {
  106.     $grp=$id/$opt_groups;
  107.     $region=chr(65+$id%$opt_groups);
  108.     do_query($dbh,"insert into $table values ($id,$rev_id,'$region',$grp,0)");
  109.   }
  110.  
  111.   $dbh->commit if (!$auto_commit);
  112.   $end_time=new Benchmark;
  113.   print "Time for $test_name  ($opt_loop_count): " .
  114.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  115. }
  116.  
  117. ###
  118. ### Test rollback performance
  119. ###
  120.  
  121. print "Test transactions rollback performance\n" if($opt_debug);
  122.  
  123. ##
  124. ## Insert rollback test
  125. ##
  126.  
  127. #
  128. # Test is done by inserting 100 rows in a table with lots of rows and
  129. # then doing a rollback on these
  130. #
  131.  
  132. {
  133.   my ($id,$rev_id,$grp,$region,$end,$loop_time,$end_time,$commit_loop,$count);
  134.  
  135.   $dbh->{AutoCommit} = 0;
  136.   $loop_time=new Benchmark;
  137.   $end=$opt_loop_count*2;
  138.   $count=0;
  139.  
  140.   for ($commit_loop=1, $id=$opt_loop_count ; $id < $end ;
  141.        $id++, $commit_loop++)
  142.   {
  143.     $rev_id=$end-$id;
  144.     $grp=$id/$opt_groups;
  145.     $region=chr(65+$id%$opt_groups);
  146.     do_query($dbh,"insert into bench1 values ($id,$rev_id,'$region',$grp,0)");
  147.     if ($commit_loop >= $opt_medium_loop_count)
  148.     {
  149.       $dbh->rollback;
  150.       $commit_loop=0;
  151.       $count++;
  152.     }
  153.   }
  154.   if ($commit_loop > 1)
  155.   {
  156.     $dbh->rollback;
  157.     $count++;
  158.   }
  159.   $end_time=new Benchmark;
  160.   print "Time for insert_rollback ($count:$opt_loop_count): " .
  161.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  162. }
  163.  
  164. ##
  165. ## Update rollback test
  166. ##
  167.  
  168. #
  169. # Test is done by updating 100 rows in a table with lots of rows and
  170. # then doing a rollback on these
  171. #
  172.  
  173. {
  174.   my ($id,$loop_time,$end_time,$commit_loop,$count);
  175.  
  176.   $dbh->{AutoCommit} = 0;
  177.   $loop_time=new Benchmark;
  178.   $end=$opt_loop_count*2;
  179.   $count=0;
  180.  
  181.   for ($commit_loop=1, $id=0 ; $id < $opt_loop_count ; $id++, $commit_loop++)
  182.   {
  183.     do_query($dbh,"update bench1 set updated=2 where idn=$id");
  184.     if ($commit_loop >= $opt_medium_loop_count)
  185.     {
  186.       $dbh->rollback;
  187.       $commit_loop=0;
  188.       $count++;
  189.     }
  190.   }
  191.   if ($commit_loop > 1)
  192.   {
  193.     $dbh->rollback;
  194.     $count++;
  195.   }
  196.   $end_time=new Benchmark;
  197.   print "Time for update_rollback ($count:$opt_loop_count): " .
  198.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  199. }
  200.  
  201. ##
  202. ## Delete rollback test
  203. ##
  204.  
  205. #
  206. # Test is done by deleting 100 rows in a table with lots of rows and
  207. # then doing a rollback on these
  208. #
  209.  
  210. {
  211.   my ($id,$loop_time,$end_time,$commit_loop,$count);
  212.  
  213.   $dbh->{AutoCommit} = 0;
  214.   $loop_time=new Benchmark;
  215.   $end=$opt_loop_count*2;
  216.   $count=0;
  217.  
  218.   for ($commit_loop=1, $id=0 ; $id < $opt_loop_count ; $id++, $commit_loop++)
  219.   {
  220.     do_query($dbh,"delete from bench1 where idn=$id");
  221.     if ($commit_loop >= $opt_medium_loop_count)
  222.     {
  223.       $dbh->rollback;
  224.       $commit_loop=0;
  225.       $count++;
  226.     }
  227.   }
  228.   if ($commit_loop > 1)
  229.   {
  230.     $dbh->rollback;
  231.     $count++;
  232.   }
  233.   $end_time=new Benchmark;
  234.   print "Time for delete_rollback ($count:$opt_loop_count): " .
  235.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  236. }
  237.  
  238.  
  239. ###
  240. ### Test update perfomance
  241. ###
  242.  
  243. test_update("bench1","update_commit",0);
  244. test_update("bench2","update_autocommit",1);
  245.  
  246. sub test_update
  247. {
  248.   my ($table, $test_name, $auto_commit)= @_;
  249.   my ($loop_time,$end_time,$id);
  250.  
  251.   $dbh->{AutoCommit}= $auto_commit;
  252.   $loop_time=new Benchmark;
  253.  
  254.   for ($id=0 ; $id < $opt_loop_count ; $id++)
  255.   {
  256.     do_query($dbh,"update $table set updated=1 where idn=$id");
  257.   }
  258.  
  259.   $dbh->commit if (!$auto_commit);
  260.   $end_time=new Benchmark;
  261.   print "Time for $test_name  ($opt_loop_count): " .
  262.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  263. }
  264.  
  265. ###
  266. ### Test delete perfomance
  267. ###
  268.  
  269. test_delete("bench1","delete_commit",0);
  270. test_delete("bench2","delete_autocommit",1);
  271.  
  272. sub test_delete
  273. {
  274.   my ($table, $test_name, $auto_commit)= @_;
  275.   my ($loop_time,$end_time,$id);
  276.  
  277.   $dbh->{AutoCommit}= $auto_commit;
  278.   $loop_time=new Benchmark;
  279.  
  280.   for ($id=0 ; $id < $opt_loop_count ; $id++)
  281.  {
  282.     do_query($dbh,"delete from $table where idn=$id");
  283.   }
  284.   $dbh->commit if (!$auto_commit);
  285.   $end_time=new Benchmark;
  286.   print "Time for $test_name  ($opt_loop_count): " .
  287.    timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  288. }
  289.  
  290. ####
  291. #### End of benchmark
  292. ####
  293.  
  294. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'}) or die $DBI::errstr;
  295. $sth = $dbh->do("drop table bench2" . $server->{'drop_attr'}) or die $DBI::errstr;
  296.  
  297. $dbh->disconnect;                # close connection
  298. end_benchmark($start_time);
  299.